package com.atgouwu.mall.product.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import com.atgouwu.mall.product.vo.SkuItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atgouwu.mall.product.entity.SkuInfoEntity;
import com.atgouwu.mall.product.service.SkuInfoService;
import com.atgouwu.mall.common.utils.PageUtils;
import com.atgouwu.mall.common.utils.R;

/**
 * sku信息
 *
 * @author heran
 * @email sunlightcs@gmail.com
 * @date 2024-02-22 15:17:48
 */
@Api(tags = "sku信息")
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = skuInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("data", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.save(skuInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.updateById(skuInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));
        return R.ok();
    }

    //获取banner轮播图
    @ApiOperation(value = "获取banner轮播图", notes = "首页根据'排序字段和查询条数'获取大banner轮播图")
    @GetMapping("/api/bannerList")
    public R bannerList(@ApiParam(value = "排序字段", defaultValue = "sale_count", required = true) @RequestParam("sortField") String sortField, @ApiParam(value = "查询数量", defaultValue = "3", required = true) @RequestParam("limit") Integer limit) {
        List<SkuInfoEntity> bannerList = skuInfoService.bannerList(sortField, limit);
        return R.ok().put("data", bannerList);
    }

    //获取今日推荐
    @ApiOperation(value = "获取今日推荐", notes = "首页根据'排序字段和查询条数'获取今日推荐")
    @GetMapping("/api/todayRecommend")
    public R todayRecommend(@ApiParam(value = "排序字段", defaultValue = "sale_count", required = true) @RequestParam("sortField") String sortField, @ApiParam(value = "查询数量", defaultValue = "4", required = true) @RequestParam("limit") Integer limit) {
        List<SkuInfoEntity> todayRecommend = skuInfoService.todayRecommend(sortField, limit);
        return R.ok().put("data", todayRecommend);
    }

    //获取热卖排行,特价排行,新品排行
    @ApiOperation(value = "获取热卖排行,特价排行,新品排行", notes = "首页根据'排行类型和查询条数'获取热卖排行,特价排行,新品排行")
    @GetMapping("/api/hotSellList")
    public R hotSellList(@ApiParam(value = "排行类型", defaultValue = "sale_count", required = true) @RequestParam("rankType") String rankType, @ApiParam(value = "查询数量", defaultValue = "4", required = true) @RequestParam("limit") Integer limit) {
        List<SkuInfoEntity> hotSellList = skuInfoService.hotSellList(rankType, limit);
        return R.ok().put("data", hotSellList);
    }

    //获取猜你喜欢
    @ApiOperation(value = "获取猜你喜欢", notes = "首页根据'排序字段和查询条数'获取猜你喜欢")
    @GetMapping("/api/likeList")
    public R likeList(@ApiParam(value = "排序字段", defaultValue = "sale_count", required = true) @RequestParam("sortField") String sortField, @ApiParam(value = "查询数量", defaultValue = "6", required = true) @RequestParam("limit") Integer limit) {
        List<SkuInfoEntity> likeList = skuInfoService.likeList(sortField, limit);
        return R.ok().put("data", likeList);
    }

    //获取家用电器,手机通讯
    @ApiOperation(value = "获取家用电器,手机通讯", notes = "首页根据'分类id和查询条数'获取家用电器,手机通讯")
    @GetMapping("/api/floorList")
    public R floorList(@ApiParam(value = "分类id", defaultValue = "225,225", required = true) @RequestParam("categoryIds") String categoryIds, @ApiParam(value = "查询数量", defaultValue = "6", required = true) @RequestParam("limit") Integer limit) {
        List<Map<String, Object>> floorList = skuInfoService.floorList(categoryIds, limit); //根据分类id获取商品
        return R.ok().put("data", floorList);
    }
//    @GetMapping("/floorList")
//    public R floorList(String catalogNames, Integer limit) {
//        List<Long> categoryIds = categoryService.getCategoryIdByName(catalogNames); //根据分类名称获取分类id
//        List<SkuInfoEntity> floorList = skuInfoService.floorList(categoryIds, limit); //根据分类id获取商品
//        return R.ok().put("data", floorList);
//    }

    //根据skuId获取商品详情
    @ApiOperation(value = "根据skuId获取商品详情")
    @GetMapping("/api/{skuId}")
    public R skuItem(@ApiParam(value = "skuId", example = "1", required = true) @PathVariable("skuId") Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo vos = skuInfoService.item(skuId);
        return R.ok().put("data", vos);
    }

    //根据skuId查询当前商品的最新价格
    @GetMapping(value = "/{skuId}/price")
    public BigDecimal getPrice(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId); //获取当前商品的信息
        BigDecimal price = skuInfo.getPrice(); //获取商品的价格
        return price;
    }

}
